package com.undsf.crawler

import org.ini4j.Config
import org.ini4j.Ini
import sun.security.util.Resources

/**
 * Created by Arathi on 2017/9/13.
 */
class CrawlerConfig {
    private static CrawlerConfig instance

    private Ini configIni

    private CrawlerConfig() {
        Config ini4jConf = new Config()
        configIni = new Ini()
        configIni.setConfig(ini4jConf)

        URL configIniUrl = Class.getResource("/config.ini")
        configIni.load(configIniUrl)
    }

    static def GetInstance() {
        if (instance == null) {
            instance = new CrawlerConfig()
        }
        return instance
    }

    String getString(String section, String key, String defaultVal) {
        String value = configIni.get(section, key)
        if (value == null) value = defaultVal
        return value
    }

    int getInteger(String section, String key, int defaultVal) {
        int digitalVal
        String value = configIni.get(section, key)
        if (value != null) {
            try {
                digitalVal = Integer.parseInt(value)
            }
            catch (NumberFormatException ex) {
                digitalVal = defaultVal
            }
        }
        else {
            digitalVal = defaultVal
        }
        return digitalVal
    }
}
